package com.appspot.ldsdrawingtool.client.raphaelgwt;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasMouseWheelHandlers;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.hydro4ge.raphaelgwt.client.PathBuilder;
import com.hydro4ge.raphaelgwt.client.Raphael;

/**
 * simple drawing demonstration
 */
public class MyDrawing extends Raphael {

    /* center point of the drawing */
    private int cx;
    private int cy;

    /**
     * a clickable, mousewheel-able circle
     */
    public class Circle extends Raphael.Circle implements HasClickHandlers, HasMouseWheelHandlers {
        public Circle(double x, double y, double node_size) {
            super(x, y, node_size);
        }

        public HandlerRegistration addClickHandler(ClickHandler handler) {
            return this.addDomHandler(handler, ClickEvent.getType());
        }

        public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
            return this.addDomHandler(handler, MouseWheelEvent.getType());
        }
    }
    
    /**
     * a clickable, mousewheel-able Path
     */
    public class MyPath extends Path implements HasClickHandlers, HasMouseWheelHandlers {
        public MyPath(PathBuilder pb) {
            super(pb);
        }
        
        public HandlerRegistration addClickHandler(ClickHandler handler) {
            return this.addDomHandler(handler, ClickEvent.getType());
        }
        
        public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
            return this.addDomHandler(handler, MouseWheelEvent.getType());
        }
    }

    /**
     * MyDrawing constructor
     */
    public MyDrawing(final int width, final int height) {
        super(width, height);
        this.cx = width / 2;
        this.cy = height / 2;
    }

    /**
     * Raphael Text and the Firefox 3.6 SVG implementation do not work together
     * when the text is appended to the drawing before the drawing is appended
     * to the document. Therefore, we defer the layout to onLoad() here instead
     * of doing it in the constructor.
     */
    @Override
    public void onLoad() {
        super.onLoad();

        // clickable, mousewheel-able circle with 200px radius
        final Circle c = new Circle(cx, cy, 200);

        // path shaped like an arrow (not clickable)
        PathBuilder pb = new PathBuilder();
        pb.M(cx, cy).m(-60, -20).l(80, 0, 0, -40, 70, 60, -70, 60, 0, -40, -80, 0).z();

        final MyPath p = new MyPath(pb);
        setFill(false, c, p);

        final Text t = new Text(cx, cy, "Example of RaphaelGWT\nClick and mouse wheel!");
        t.attr("font-size", "10px");

        ClickHandler clickHandler = new ClickHandler() {
            private boolean toggle = true;

            public void onClick(ClickEvent e) {
                setFill(toggle, c, p);
                toggle = !toggle;
            }
        };
        p.addClickHandler(clickHandler);
        c.addClickHandler(clickHandler);
        
        MouseWheelHandler mwh = new MouseWheelHandler() {
            private final static int DEGREES = 20;

            public void onMouseWheel(MouseWheelEvent e) {
                p.rotate((e.isNorth() ? -1 : 1) * DEGREES, cx, cy, false);
                t.rotate((e.isNorth() ? -1 : 1) * DEGREES, cx, cy, false);
            }
        };
        
        p.addMouseWheelHandler(mwh);
        c.addMouseWheelHandler(mwh);
    }

    private void setFill(boolean swap, Circle c, Path p) {
        if (swap) {
            c.attr("fill", "#d1b48c");
            p.attr("fill", "#974b00");
        } else {
            c.attr("fill", "#974b00");
            p.attr("fill", "#d1b48c");
        }
    }

}
